package com.kchouse.homepage;

import android.annotation.SuppressLint;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.alibaba.fastjson.JSONObject;
import com.kchouse.MainActivity;
import com.kchouse.R;
import com.kchouse.StartActivity;
import com.kchouse.bean.HostItem;
import com.kchouse.bean.JobItem;
import com.kchouse.common.API;
import com.kchouse.common.BaseFragment;
import com.kchouse.common.MyHttpCycleContext;
import com.kchouse.common.ToastUtils;
import com.kchouse.myadapter.JobAdapter;
import com.kchouse.myadapter.MerchantAdapter;

import java.util.ArrayList;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;
import me.fangx.haorefresh.HaoRecyclerView;
import me.fangx.haorefresh.LoadMoreListener;

public class JobFragment extends BaseFragment {

    private int single;
    private String name;
    private String id;
    private LayoutInflater inflater;
    private View view;
    private ArrayList<JobItem> listjob = new ArrayList();
    private HaoRecyclerView hao_recycleview;
    private View rl_empty;
    private JobAdapter adapter;
    private int distanceStatus=1;//距离排序
    private int salaryStatus=1;//工资排序
    private CheckBox cb_1;
    private CheckBox cb_2;

    public JobFragment() {
        // Required empty public constructor
    }

    @SuppressLint("ValidFragment")//项目id 名称 排序（可不用）
    public JobFragment(String id) {
        this.id = id;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (view == null) {
            this.inflater = inflater;
            view = inflater.inflate(R.layout.fragment_job, null);
            intiData();
            intiView();
        }

        return view;
    }

    private void intiView() {



        hao_recycleview = (HaoRecyclerView) view.findViewById(R.id.hao_recycleview);
        rl_empty = view.findViewById(R.id.rl_empty);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        hao_recycleview.setLayoutManager(layoutManager);
        adapter = new JobAdapter(getActivity(), listjob);
        hao_recycleview.setAdapter(adapter);

//        hao_recycleview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                HostItem item = listmerchant.get(position);
//                intent.putExtra("isDir", false);
//                intent.putExtra("id", item.getId());
//                intent.setClass(getActivity(), GoodsDetailsActivity.class);
//                startActivity(intent);
//            }
//        });


        cb_1= (CheckBox)view.findViewById(R.id.cb_1);
        cb_1.setChecked(true);
        Drawable drawable1 = getResources().getDrawable(R.mipmap.ic_red_down);
        drawable1.setBounds(0, 0, 22, 22);
        cb_1.setCompoundDrawablePadding(8);
        cb_1.setCompoundDrawables(null,null,drawable1,null);
        cb_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    distanceStatus=1;
                    Drawable drawable = getResources().getDrawable(R.mipmap.ic_red_down);
                    drawable.setBounds(0, 0, 22, 22);
                    cb_1.setCompoundDrawablePadding(8);
                    cb_1.setCompoundDrawables(null,null,drawable,null);
                    cb_1.setTextColor(getResources().getColor(R.color.colorred));
                    cb_2.setTextColor(getResources().getColor(R.color.colorblack));
                }else {
                    salaryStatus=0;
                    Drawable drawable = getResources().getDrawable(R.mipmap.ic_red_up);
                    drawable.setBounds(0, 0, 22, 22);
                    cb_1.setCompoundDrawablePadding(8);
                    cb_1.setCompoundDrawables(null,null,drawable,null);
                    cb_1.setTextColor(getResources().getColor(R.color.colorred));
                    cb_2.setTextColor(getResources().getColor(R.color.colorblack));
                }
                GetJobList(distanceStatus, salaryStatus);
            }
        });
        cb_2= (CheckBox)view.findViewById(R.id.cb_2);
        cb_2.setChecked(true);
        Drawable drawable2 = getResources().getDrawable(R.mipmap.ic_gray_down);
        drawable2.setBounds(0, 0, 22, 18);
        cb_2.setCompoundDrawablePadding(8);
        cb_2.setCompoundDrawables(null,null,drawable2,null);
        cb_2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    distanceStatus=1;
                    Drawable drawable = getResources().getDrawable(R.mipmap.ic_gray_down);
                    drawable.setBounds(0, 0, 22, 18);
                    cb_2.setCompoundDrawablePadding(8);
                    cb_2.setCompoundDrawables(null,null,drawable,null);
                    cb_2.setTextColor(getResources().getColor(R.color.colorred));
                    cb_1.setTextColor(getResources().getColor(R.color.colorblack));
                }else {
                    distanceStatus=0;
                    Drawable drawable = getResources().getDrawable(R.mipmap.ic_gray_up);
                    drawable.setBounds(0, 0, 22, 18);
                    cb_2.setCompoundDrawablePadding(8);
                    cb_2.setCompoundDrawables(null,null,drawable,null);
                    cb_2.setTextColor(getResources().getColor(R.color.colorred));
                    cb_1.setTextColor(getResources().getColor(R.color.colorblack));
                }
                GetJobList(distanceStatus, salaryStatus);
            }
        });
    }

    private void intiData() {
        GetJobList(distanceStatus, salaryStatus);
    }

    /**
     * 招聘列表
     */
    private void GetJobList( int distanceStatus,  int salaryStatus) {
        String url = API.GetJobList;
        RequestParams params = new RequestParams(new MyHttpCycleContext());//请求参数
        params.addFormDataPart("typeId", id);
//        params.addFormDataPart("page", page);
//        params.addFormDataPart("pageSize", 10);
        params.addFormDataPart("lat", StartActivity.mLatitude + "");
        params.addFormDataPart("log", StartActivity.mLongitude + "");
        params.addFormDataPart("distanceStatus", distanceStatus);//排序-1/不排序-0
        params.addFormDataPart("salaryStatus", salaryStatus);//排序-1/不排序-0

        HttpRequest.get(url, params, new BaseHttpRequestCallback<String>() {

            //请求网络前
            @Override
            public void onStart() {
                showdialog();
            }

            @Override
            protected void onSuccess(String str) {
                Log.e("==", str);
                JSONObject json = JSONObject.parseObject(str);
                if (json.getBoolean("successful")) {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
//                    totalCount = data.getInteger("totalCount");
//
//                    if (listmerchant.size() == totalCount)//总个数已满
//                    {
//                        hao_recycleview.setCanloadMore(false);
//                        ToastUtils.getInstanc(getActivity()).showToast("暂无更多");
//                    } else {
                    listjob.clear();
                    listjob.addAll(JSONObject.parseArray(data.getString("jobs"), JobItem.class));
                    adapter.notifyDataSetChanged();
//                    }
                    if(listjob.size() == 0) {
                        rl_empty.setVisibility(View.VISIBLE);
                    }
                } else {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    ToastUtils.getInstanc(getActivity()).showToast(data.getString("msg"));
                }
            }

            //请求失败（服务返回非法JSON、服务器异常、网络异常）
            @Override
            public void onFailure(int errorCode, String msg) {
//                toast("网络异常~，请检查你的网络是否连接后再试");
                Log.e("==", msg);
            }

            //请求网络结束
            @Override
            public void onFinish() {
                dismissdialog();
            }
        });
    }

}
